using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	#endregion
	public class NGUOIDUNGRepository
	{
		public static List<NGUOIDUNG> SelectAll()
		{
			return DataContext.Instance.NGUOIDUNG.OrderBy(item => item.TEN_DANG_NHAP).ToList();
		}

		public static NGUOIDUNG SelectByID(int maNGUOIDUNG)
		{
			return DataContext.Instance.NGUOIDUNG.FirstOrDefault(item => item.NGUOIDUNG_ID == maNGUOIDUNG );
		}

		public static bool Insert(NGUOIDUNG obj)
		{
			try
			{
				DataContext.Instance.NGUOIDUNG.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int maNGUOIDUNG)
		{
			try
			{
				var delitem = DataContext.Instance.NGUOIDUNG.FirstOrDefault(item => item.NGUOIDUNG_ID == maNGUOIDUNG );
				DataContext.Instance.NGUOIDUNG.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<NGUOIDUNG> RetrieveByID(int maNGUOIDUNG)
		{
			return (from item in DataContext.Instance.NGUOIDUNG where  item.NGUOIDUNG_ID == maNGUOIDUNG  select item).ToList();
		}

		public static List<NGUOIDUNG> SelectByMaLoaiNGUOIDUNG(int maloaiNGUOIDUNG)
		{
			var lstItem = (from item in DataContext.Instance.NGUOIDUNG where item.LOAI_NGUOI_DUNG_ID == maloaiNGUOIDUNG select item).ToList();
			return lstItem;
		}

        public static NGUOIDUNG SelectByUserName(string username)
        {
            return DataContext.Instance.NGUOIDUNG.FirstOrDefault(item => item.TEN_DANG_NHAP == username);
        }

        public static NGUOIDUNG SelectByUserNameAndPassword(string username, string password)
        {
            return DataContext.Instance.NGUOIDUNG.FirstOrDefault(item => item.TEN_DANG_NHAP == username && item.MATKHAU == password);
        }
	}
}